package com.copyctrl.consumer.listener;

import cn.hutool.json.JSONUtil;
import com.copyctrl.campus.common.domain.MysqlData;
import com.copyctrl.campus.common.domain.SendData;
import com.copyctrl.consumer.utils.MysqlUtil;
import com.copyctrl.consumer.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;

@Component
@Slf4j
public class SpringRabbitListener {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    MysqlUtil util;



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "redis.queue"),
            exchange = @Exchange(name = "direct.data.exchange", type = ExchangeTypes.DIRECT),
            key = {"redis"}
    ))
    public void listenRedisQueue(String msg) throws InterruptedException {
        System.out.println("消费者接收到redis.queue的消息：【" + msg + "】");
        SendData data = JSONUtil.toBean(msg, SendData.class);
        try {
            RedisUtil.saveData(data.getRedisData(),stringRedisTemplate);
        } catch (NoSuchMethodException e) {
            Thread.sleep(1000000);
            log.info("遇到异常了，等待管理员处理");
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            Thread.sleep(1000000);
            log.info("遇到异常了，等待管理员处理");
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            Thread.sleep(1000000);
            log.info("遇到异常了，等待管理员处理");
            throw new RuntimeException(e);
        }
        log.info("成功消费消息队列中的消息msg:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "mysql.queue"),
            exchange = @Exchange(name = "direct.data.exchange", type = ExchangeTypes.DIRECT),
            key = {"mysql"}
    ))
    public void listenMysqlQueue(String msg){
        SendData data=JSONUtil.toBean(msg, SendData.class);
        MysqlData mysqlData = data.getMysqlData();
        try {
           util.executeData(mysqlData);
        } catch (NoSuchMethodException e) {
            log.error("mapper中没有对应的方法");
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            log.error("方法执行失败");
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        log.info("成功消费消息队列中的消息msg:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "file.queue"),
            exchange = @Exchange(name = "direct.data.exchange", type = ExchangeTypes.DIRECT),
            key = {"file"}
    ))
    public void listenFileQueue(String msg){


        log.info("成功消费消息队列中的消息msg:"+msg);
    }
}
